<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Chapter 38. Boost.TypeErasure</title>
<link rel="stylesheet" href="../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="index.html" title="The Boost C++ Libraries BoostBook Documentation Subset">
<link rel="up" href="libraries.html" title="Part I. The Boost C++ Libraries (BoostBook Subset)">
<link rel="prev" href="thread/compliance.html" title="Conformance and Extension">
<link rel="next" href="boost_typeerasure/reading.html" title="How to read this documentation">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../boost.png"></td>
<td align="center"><a href="../../index.html">Home</a></td>
<td align="center"><a href="../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="thread/compliance.html"><img src="../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="libraries.html"><img src="../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="index.html"><img src="../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="boost_typeerasure/reading.html"><img src="../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="chapter">
<div class="titlepage"><div>
<div><h2 class="title">
<a name="boost_typeerasure"></a>Chapter 38. Boost.TypeErasure</h2></div>
<div><div class="author"><h3 class="author">
<span class="firstname">Steven</span> <span class="surname">Watanabe</span>
</h3></div></div>
<div><p class="copyright">Copyright © 2011-2013 Steven Watanabe</p></div>
<div><div class="legalnotice">
<a name="boost_typeerasure.legal"></a><p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></div>
</div></div>
<div class="toc">
<p><b>Table of Contents</b></p>
<dl class="toc">
<dt><span class="section"><a href="boost_typeerasure.html#boost_typeerasure.introduction">Introduction</a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/reading.html">How to read this documentation</a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/basic.html">Basic Usage</a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/composing_concepts.html">Composing Concepts</a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/multi.html">Functions with Multiple Arguments</a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/concept.html">Concepts in Depth</a></span></dt>
<dd><dl>
<dt><span class="section"><a href="boost_typeerasure/concept.html#boost_typeerasure.concept.custom">Defining Custom Concepts</a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/concept.html#boost_typeerasure.concept.overload">Overloading</a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/concept.html#boost_typeerasure.concept.concept_map">Concept Maps</a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/concept.html#boost_typeerasure.concept.overload0">Associated Types</a></span></dt>
</dl></dd>
<dt><span class="section"><a href="boost_typeerasure/any.html">Using Any</a></span></dt>
<dd><dl>
<dt><span class="section"><a href="boost_typeerasure/any.html#boost_typeerasure.any.construction">Construction</a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/any.html#boost_typeerasure.any.conversions">Conversions</a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/any.html#boost_typeerasure.any.references">References</a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/any.html#boost_typeerasure.any.limit">Syntax Limitations</a></span></dt>
</dl></dd>
<dt><span class="section"><a href="boost_typeerasure/examples.html">Examples</a></span></dt>
<dd><dl>
<dt><span class="section"><a href="boost_typeerasure/examples.html#boost_typeerasure.examples.print_sequence">A polymorphic
      range formatter</a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/examples.html#boost_typeerasure.examples.printf">A type-safe printf</a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/examples.html#boost_typeerasure.examples.multifunction">Boost.Function
      with multiple signatures</a></span></dt>
</dl></dd>
<dt><span class="section"><a href="boost_typeerasure/conceptdef.html">Concept Definitions</a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/predef.html">Predefined Concepts</a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/reference.html">Reference</a></span></dt>
<dd><dl>
<dt><span class="section"><a href="boost_typeerasure/reference.html#doxygen.any_8hpp">Header &lt;boost/type_erasure/any.hpp&gt;</a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/reference.html#doxygen.any__cast_8hpp">Header &lt;boost/type_erasure/any_cast.hpp&gt;</a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/reference.html#doxygen.binding_8hpp">Header &lt;boost/type_erasure/binding.hpp&gt;</a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/reference.html#doxygen.binding__of_8hpp">Header &lt;boost/type_erasure/binding_of.hpp&gt;</a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/reference.html#doxygen.builtin_8hpp">Header &lt;boost/type_erasure/builtin.hpp&gt;</a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/reference.html#doxygen.call_8hpp">Header &lt;boost/type_erasure/call.hpp&gt;</a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/reference.html#doxygen.callable_8hpp">Header &lt;boost/type_erasure/callable.hpp&gt;</a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/reference.html#doxygen.check__match_8hpp">Header &lt;boost/type_erasure/check_match.hpp&gt;</a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/reference.html#doxygen.concept__interface_8hpp">Header &lt;boost/type_erasure/concept_interface.hpp&gt;</a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/reference.html#doxygen.concept__of_8hpp">Header &lt;boost/type_erasure/concept_of.hpp&gt;</a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/reference.html#doxygen.config_8hpp">Header &lt;boost/type_erasure/config.hpp&gt;</a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/reference.html#doxygen.constructible_8hpp">Header &lt;boost/type_erasure/constructible.hpp&gt;</a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/reference.html#doxygen.deduced_8hpp">Header &lt;boost/type_erasure/deduced.hpp&gt;</a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/reference.html#doxygen.derived_8hpp">Header &lt;boost/type_erasure/derived.hpp&gt;</a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/reference.html#doxygen.dynamic__any__cast_8hpp">Header &lt;boost/type_erasure/dynamic_any_cast.hpp&gt;</a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/reference.html#doxygen.dynamic__binding_8hpp">Header &lt;boost/type_erasure/dynamic_binding.hpp&gt;</a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/reference.html#doxygen.exception_8hpp">Header &lt;boost/type_erasure/exception.hpp&gt;</a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/reference.html#doxygen.free_8hpp">Header &lt;boost/type_erasure/free.hpp&gt;</a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/reference.html#doxygen.is__empty_8hpp">Header &lt;boost/type_erasure/is_empty.hpp&gt;</a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/reference.html#doxygen.is__placeholder_8hpp">Header &lt;boost/type_erasure/is_placeholder.hpp&gt;</a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/reference.html#doxygen.is__subconcept_8hpp">Header &lt;boost/type_erasure/is_subconcept.hpp&gt;</a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/reference.html#doxygen.iterator_8hpp">Header &lt;boost/type_erasure/iterator.hpp&gt;</a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/reference.html#doxygen.member_8hpp">Header &lt;boost/type_erasure/member.hpp&gt;</a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/reference.html#doxygen.operators_8hpp">Header &lt;boost/type_erasure/operators.hpp&gt;</a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/reference.html#doxygen.param_8hpp">Header &lt;boost/type_erasure/param.hpp&gt;</a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/reference.html#doxygen.placeholder_8hpp">Header &lt;boost/type_erasure/placeholder.hpp&gt;</a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/reference.html#doxygen.placeholder__of_8hpp">Header &lt;boost/type_erasure/placeholder_of.hpp&gt;</a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/reference.html#doxygen.rebind__any_8hpp">Header &lt;boost/type_erasure/rebind_any.hpp&gt;</a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/reference.html#doxygen.register__binding_8hpp">Header &lt;boost/type_erasure/register_binding.hpp&gt;</a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/reference.html#doxygen.relaxed_8hpp">Header &lt;boost/type_erasure/relaxed.hpp&gt;</a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/reference.html#doxygen.require__match_8hpp">Header &lt;boost/type_erasure/require_match.hpp&gt;</a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/reference.html#doxygen.same__type_8hpp">Header &lt;boost/type_erasure/same_type.hpp&gt;</a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/reference.html#doxygen.static__binding_8hpp">Header &lt;boost/type_erasure/static_binding.hpp&gt;</a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/reference.html#doxygen.tuple_8hpp">Header &lt;boost/type_erasure/tuple.hpp&gt;</a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/reference.html#doxygen.typeid__of_8hpp">Header &lt;boost/type_erasure/typeid_of.hpp&gt;</a></span></dt>
</dl></dd>
<dt><span class="section"><a href="boost_typeerasure/rationale.html">Rationale</a></span></dt>
<dd><dl>
<dt><span class="section"><a href="boost_typeerasure/rationale.html#boost_typeerasure.rationale.why_do_i_have_to_specify_the_presence_of_a_destructor_explicitly_">Why
      do I have to specify the presence of a destructor explicitly?</a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/rationale.html#boost_typeerasure.rationale.why_non_member_functions_">Why
      non-member functions?</a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/rationale.html#boost_typeerasure.rationale.placeholder">Why are the
      placeholders called <code class="computeroutput"><span class="identifier">_a</span></code>, <code class="computeroutput"><span class="identifier">_b</span></code> and not <code class="computeroutput"><span class="identifier">_1</span></code>
      <code class="computeroutput"><span class="identifier">_2</span></code></a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/rationale.html#boost_typeerasure.rationale.ref">Why not use <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">ref</span></code>
      for references?</a></span></dt>
</dl></dd>
<dt><span class="section"><a href="boost_typeerasure/future.html">Future Work</a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/acknowledgements.html">Acknowledgements</a></span></dt>
<dt><span class="section"><a href="boost_typeerasure/related.html">Related Work</a></span></dt>
</dl>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="boost_typeerasure.introduction"></a><a class="link" href="boost_typeerasure.html#boost_typeerasure.introduction" title="Introduction">Introduction</a>
</h2></div></div></div>
<p>
      The Boost.TypeErasure library provides runtime polymorphism in C++ that is
      more flexible than that provided by the core language.
    </p>
<p>
      C++ has two distinct kinds of polymorphism, virtual functions and templates,
      each of which has its own advantages and disadvantages.
    </p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
          Virtual functions are not resolved until runtime, while templates are always
          resolved at compile time. If your types can vary at runtime (for example,
          if they depend on user input), then static polymorphism with templates
          doesn't help much.
        </li>
<li class="listitem">
          Virtual functions can be used with separate compilation. The body of a
          template has to be available in every translation unit in which it is used,
          slowing down compiles and increasing rebuilds.
        </li>
<li class="listitem">
          Virtual functions automatically make the requirements on the arguments
          explicit. Templates are only checked when they're instantiated, requiring
          extra work in testing, assertions, and documentation.
        </li>
<li class="listitem">
          The compiler creates a new copy of each function template every time it
          is instantiated. This allows better optimization, because the compiler
          knows everything statically, but it also causes a significant increase
          of binary sizes.
        </li>
<li class="listitem">
          Templates support Value semantics. Objects that "behave like an int"
          and are not shared are easier to reason about. To use virtual functions,
          on the other hand, you have to use (smart) pointers or references.
        </li>
<li class="listitem">
          Template libraries can allow third-party types to be adapted non-intrusively
          for seamless interoperability. With virtual functions, you have to create
          a wrapper that inherits from the base class.
        </li>
<li class="listitem">
          Templates can handle constraints involving multiple types. For example,
          std::for_each takes an iterator range and a function that can be called
          on the elements of the range. Virtual functions aren't really able to express
          such constraints.
        </li>
</ul></div>
<p>
      The Boost.TypeErasure library combines the superior abstraction capabilities
      of templates, with the runtime flexibility of virtual functions.
    </p>
<p>
      Boost includes several special cases of this kind of polymorphism:
    </p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
          <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">any</span></code> for CopyConstructible types.
        </li>
<li class="listitem">
          <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">function</span></code> for objects that can be called
          like functions.
        </li>
<li class="listitem">
          Boost.Range provides <code class="computeroutput"><span class="identifier">any_iterator</span></code>.
        </li>
</ul></div>
<p>
      Boost.TypeErasure generalizes this to support arbitrary requirements and provides
      a <a class="link" href="boost_typeerasure/predef.html" title="Predefined Concepts">predefined set of common concepts</a>
    </p>
</div>
</div>
<div class="copyright-footer"></div>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="thread/compliance.html"><img src="../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="libraries.html"><img src="../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="index.html"><img src="../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="boost_typeerasure/reading.html"><img src="../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
